package com.javastudy.apache.beanutils;


import com.javastudy.data.Book;
import com.javastudy.data.Person;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * Created by admin on 2017/6/15.
 */
public class BeanUtilsTest {

    Person person;
    @Before
    public void before() throws Exception {
        person = new Person();
        person.setName("Name John");
        person.setAge(29);

        Book book = new Book();
        book.setName("老人与海");
        book.setPrice(26);
        person.setBook(book);
    }

    @Test
    public void getPropertyValue() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String name = BeanUtils.getProperty(person,"name");
        String age = BeanUtils.getProperty(person,"age");
        System.out.println("name="+name);
        System.out.println("age="+age);
    }

    //需要测试各种类型的setProperty，自定义复杂类型，还有包括List,Map, Array，以及它们中拥有的是自定义复杂类型等等
    @Test
    public void setProperty() throws InvocationTargetException, IllegalAccessException {
        BeanUtils.setProperty(person,"password","ABCeetPass");
        BeanUtils.setProperty(person, "addresses[0]", "addr-1");
        System.out.println("password="+person.getPassword());
        
    }

    @Test
    public void cloneBean() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        Person clone = (Person)BeanUtils.cloneBean(person);
        System.out.println(clone.toString());
        System.out.println(person.toString());
    }

    //好像就是获取Bean的属性值而已，好像是获取对象的get/set。用到反射，获取类的属性啥的，可以学习学习
    @Test
    public void describe() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        @SuppressWarnings("unchecked")
        Map<String, String> map4 = BeanUtils.describe(person);

        System.out.println(map4.get("name"));
        System.out.println(map4.get("age"));
    }

    @Test
    public void populate() throws InvocationTargetException, IllegalAccessException {
        Person person1 = new Person();
        Map<String, String> map5 = new HashMap<String, String>();
        map5.put("name", "rensanning");
        BeanUtils.populate(person1, map5);
        System.out.println(person1.getName());
        System.out.println(person1.getAge());
    }

    @Test
    public void testBeanArrayAndCollectionProperty() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        person.setAddresses(new String[]{"Guangzhou","Beijing"});
        List<String> children = new ArrayList<>();
        children.add("Penny");
        children.add("Andy");
        person.setChildren(children);

        String[] array = BeanUtils.getArrayProperty(person, "addresses");

        System.out.println(array.length);//2
    System.out.println(array[0]);//"Guangzhou"
        System.out.println(array[1]);//"Beijing"

        String[] list = BeanUtils.getArrayProperty(person, "children");
        System.out.println(list.length);//2
        System.out.println(list[0]);//"Penny"
        System.out.println(list[1]);//"Andy"

        System.out.println(BeanUtils.getProperty(person, "addresses[0]"));//"Guangzhou"
        System.out.println(BeanUtils.getIndexedProperty(person, "addresses", 1));//"Beijing"
    }

    @Test
    public void testBeanMapProperty() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Map<String, String> books = new HashMap<String, String>();
        books.put("book1", "哈利波特");
        books.put("book2", "童年");
        person.setBookMap(books);

        String value1 = BeanUtils.getMappedProperty(person, "books", "book1");
        System.out.println(value1);//"哈利波特"

        String value2 = BeanUtils.getMappedProperty(person, "books", "book2");
        System.out.println(value2);//"童年"

        System.out.println(BeanUtils.getProperty(person, "books.book1"));//"哈利波特"
        System.out.println(BeanUtils.getProperty(person, "books.book2"));//"童年"
        System.out.println(BeanUtils.getProperty(person, "book.name"));//"老人与海"
    }

    @Test
    public void testNestedBean() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Book book = new Book("吹牛大王",49);
        person.setBook(book);

        String value = BeanUtils.getNestedProperty(person, "book.name");
        System.out.println(value);//"xxx"

//        System.out.println(BeanUtils.getProperty(person, "book.nestedProperty"));//"xxx"
    }

    //需要看看这个是怎么用的:http://www.cnblogs.com/chenpi/p/6920896.html#_label2
    @Test
    public void testDateConvertor() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        DateConverter converter = new DateConverter();
        converter.setPattern("yyyy/MM/dd HH:mm:ss");

        ConvertUtils.register(converter, Date.class);
        ConvertUtils.register(converter, String.class);

        BeanUtils.setProperty(person, "birthday", "2010/12/19 23:40:00");

        String value9 = BeanUtils.getProperty(person, "birthday");
        System.out.println(value9);//"2010/12/19 23:40:00"
    }

//    private void testOtherBean() throws InvocationTargetException, IllegalAccessException {
//        Person person2 = new Person();
//        BeanUtils.setProperty(person, "birthday", "2010/12/19 23:40:00");
//        
//    }
    
    private void otherMethods(){
//    	BeanUtils.cloneBean(bean);
//    	BeanUtils.copyProperties(dest, orig);
//    	BeanUtils.copyProperty(bean, name, value);
//    	BeanUtils.createCache();
//    	BeanUtils.describe(bean);
//    	BeanUtils.getArrayProperty(bean, name);
//    	BeanUtils.getCacheFast(map);
//    	BeanUtils.getDebug();
//    	BeanUtils.getIndexedProperty(bean, name);
//    	BeanUtils.getIndexedProperty(bean, name, index);
//    	BeanUtils.getMappedProperty(bean, name);
//    	BeanUtils.getMappedProperty(bean, name, key);
//    	BeanUtils.getNestedProperty(bean, name);
//    	BeanUtils.getProperty(bean, name);
//    	BeanUtils.getSimpleProperty(bean, name);
//    	BeanUtils.populate(bean, properties);
//    	BeanUtils.setCacheFast(map, fast);
//    	BeanUtils.setProperty(bean, name, value);
    }
}
